﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace GemBlaster.UI
{
    public class Gem : Control
    {
        public Gem()
        {
            Stream s = this.GetType().Assembly.GetManifestResourceStream("GemBlaster.UI.Gem.xaml");
            _root = this.InitializeFromXaml(new StreamReader(s).ReadToEnd());

            _bg = _root.FindName("Background") as System.Windows.Shapes.Path;
            _movement = _root.FindName("Movement") as Storyboard;
            _collapse = _root.FindName("Collapse") as Storyboard;
            _leftAnim = _root.FindName("Left") as DoubleAnimation;
            _topAnim = _root.FindName("Top") as DoubleAnimation;
            _hover = _root.FindName("Hover") as Storyboard;
            _ring = _root.FindName("SelectionRing") as System.Windows.Shapes.Path;

            _movement.Completed += new EventHandler(_movement_Completed);
            _collapse.Completed += new EventHandler(_collapse_Completed);
        }

        public void Reset()
        {
            _collapse.Stop();
            _hover.Stop();
            _movement.Stop();
        }

        public void StartHover()
        {
            _hover.Begin();
        }

        public void StopHover()
        {
            _hover.Stop();
        }

        FrameworkElement _root;
        System.Windows.Shapes.Path _bg;
        Storyboard _movement;
        Storyboard _collapse;
        DoubleAnimation _leftAnim;
        DoubleAnimation _topAnim;
        Storyboard _hover;
        System.Windows.Shapes.Path _ring;

        static double _gemSize = 26.703;
        public static double GemSize { get { return _gemSize; } }

        public Color GemColor
        {
            get
            {
                return ((SolidColorBrush)_bg.Fill).Color;
            }
            set
            {
                ((SolidColorBrush)_bg.Fill).Color = value;
            }
        }

        public double Left
        {
            get
            {
                return (double) _root.GetValue(Canvas.LeftProperty);
            }
            set
            {
                _root.SetValue<double>(Canvas.LeftProperty, value);
            }
        }

        public double Top
        {
            get
            {
                return (double)_root.GetValue(Canvas.TopProperty);
            }
            set
            {
                _root.SetValue<double>(Canvas.TopProperty, value);
            }
        }

        public void MoveTo(Point position, TimeSpan delay)
        {
            if (MoveAnimationStarting != null)
                MoveAnimationStarting(this, EventArgs.Empty);
            _movement.BeginTime = delay;
            _leftAnim.From = Left;
            _leftAnim.To = position.X;
            _topAnim.From = Top;
            _topAnim.To = position.Y;
            _movement.Begin();
        }

        void _movement_Completed(object sender, EventArgs e)
        {
            if (MoveAnimationComplete != null)
                MoveAnimationComplete(this, EventArgs.Empty);
        }

        public event EventHandler MoveAnimationComplete;
        public event EventHandler MoveAnimationStarting;

        public void Collapse(TimeSpan delay)
        {
            if (CollapseAnimationStarting != null)
                CollapseAnimationStarting(this, EventArgs.Empty);
            _collapse.BeginTime = delay;
            _collapse.Begin();
        }

        void _collapse_Completed(object sender, EventArgs e)
        {
            if (CollapseAnimationComplete != null)
                CollapseAnimationComplete(this, EventArgs.Empty);
        }

        public event EventHandler CollapseAnimationComplete;
        public event EventHandler CollapseAnimationStarting;

    }
}